import puppeteer from "puppeteer";
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs";
import { dirname } from "path";

const browser = await puppeteer.launch({
  timeout: 3000,
  headless: false,
  defaultViewport: {
    width: 0,
    height: 0,
  },
});
const page = await browser.newPage();
await page.goto(
  "https://nest-docs.liushuaiyang.com/%E7%AC%AC01%E7%AB%A0%E2%80%94%E5%BC%80%E7%AF%87%E8%AF%8D.html"
);

const menu_btn = await page.$('button[aria-controls="VPSidebarNav"]');

console.log(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const currentDir = dirname(__filename);
const htmlDir = path.join(currentDir, "html");

await menu_btn.click();
const textlist = await page.$eval(".VPSidebarItem .items", (el) => {
  const children = [...el.children];
  const text = [];
  for (let i = 0; i < children.length; i++) {
    const child = children[i];
    text.push({
      title: child.textContent,
      link: `https://nest-docs.liushuaiyang.com/${child.textContent}.html`,
    });
  }
  return text;
});

async function createMenu() {
  const template = `<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    img {
      min-width: 200px;
      min-height:200px;
    }
    </style>
  </head>
  <body>
  ${textlist.map((v) => {
    return `<div><a href="./${v.title}.html">${v.title}</a></div>`;
  }).join("")}
  </body>
  </html>`;
  const htmlName = path.join(htmlDir, `index.html`);
  fs.writeFile(htmlName, template, {}, (err) => {
    if (err) {
      console.error(err);
    } else {
      console.log(`${htmlName} :写入成功`);
    }
  });
}

createMenu();

async function createHtml() {
  for (let i = 0; i < textlist.length; i++) {
    const node = textlist[i];
    if (node.title.includes("第56章")) {
      continue;
    }
    await page.goto(node.link);
    let htmlContent = null;
    try {
      htmlContent = await page.$eval(".content-container", (el) => {
        return el.innerHTML;
      });
    } catch (e) {
      console.log("-----age.$eval-----", e);
    }

    if (!fs.existsSync(htmlDir)) {
      fs.mkdirSync(htmlDir);
    }
    // console.log("----htmlContent----", htmlContent);
    if (!htmlContent) continue;
    const template = `<!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
      img {
        min-width: 200px;
        min-height:200px;
      }
      </style>
    </head>
    <body>
    ${htmlContent?.replace(/\/\/liushuaiyang/g, "https://liushuaiyang")}
    </body>g
    </html>`;
    const htmlName = path.join(htmlDir, `${node.title}.html`);
    await fs.writeFile(htmlName, template, {}, (err) => {
      if (err) {
        console.error(err);
      } else {
        console.log(`${htmlName} :写入成功`);
      }
    });
  }
}

// createHtml();
